//
//  FMManageObject.m
//  ZAPlayer
//
//  Created by 邹奥 on 16/4/28.
//  Copyright © 2016年 zouao. All rights reserved.
//

#import "FMManageObject.h"

@implementation FMManageObject

+ (FMManageObject *)standerDefault
{
    static dispatch_once_t onceToken;
    static FMManageObject *manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}

- (id)init
{
    self = [super init];
    if (self) {
         _db = [FMDatabase databaseWithPath:[self getFilePath]] ;
    }
    return self;
}

- (NSString *)getFilePath
{
     NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
     return [filePath stringByAppendingPathComponent:@"data.db"];
}

#pragma mark - 封装
#pragma mark - 歌曲下载
//自定义文件-创建
- (BOOL)createMainTable:(NSString *)table byDB:(FMDatabase *)db//primary key
{
    //id,文件名
    return [db executeUpdate:[NSString stringWithFormat:@"create table %@(fileID text primary key ,fileName text,id text,url text,size text,type text)",table]];
}
//歌曲下载-查询
- (NSArray *)onSelectCustomMusicFileCount
{
    NSString *sql = [NSString stringWithFormat:@"select * from Main_table"];
    
    if (![_db open]) {
        NSLog(@"数据库没有打开!");
        return nil;
    }
    
    [_db setShouldCacheStatements:YES];
    
    if (![_db tableExists:@"Main_table"]) {
        if (![self createMainTable:@"Main_table" byDB:_db]) {
            NSLog(@"表单不存在!");
            return nil;
        }
    }
    
    NSMutableArray *data = [[NSMutableArray alloc]init];
    FMResultSet *result = [_db executeQuery:sql];
    while ([result next]) {
        NSString *idStr = [result stringForColumn:@"id"];
        NSString *fileName = [result stringForColumn:@"fileName"];
        NSString *fileID = [result stringForColumn:@"fileID"];
        NSString *url = [result stringForColumn:@"url"];
        NSString *size = [result stringForColumn:@"size"];
        NSString *type = [result stringForColumn:@"type"];
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:idStr,@"id",fileName,@"fileName",fileID,@"fileID",url,@"url",size,@"size",type,@"type", nil];
        [data addObject:dic];
    }
    
    if (![_db close]) {
        NSLog(@"数据库没有关闭!");
        return nil;
    }
    
    return data;
}

//自定义文件-根据文件名查询数据库是否存在
- (NSArray *)SelectTableWithFileID:(NSString *)fileName
{
    NSString *sql = [NSString stringWithFormat:@"select * from Main_table where fileID='%@'",fileName];
    if (![_db open]) {
        NSLog(@"数据库没有打开!");
        return nil;
    }
    
    [_db setShouldCacheStatements:YES];
    
    if (![_db tableExists:@"Main_table"]) {
        if (![self createMainTable:@"Main_table" byDB:_db]) {
            NSLog(@"表单不存在!");
            return nil;
        }
    }
    
    NSMutableArray *data = [[NSMutableArray alloc]init];
    FMResultSet *result = [_db executeQuery:sql];
    while ([result next]) {
        NSString *idStr = [result stringForColumn:@"id"];
        NSString *fileName = [result stringForColumn:@"fileName"];
        NSString *fileID = [result stringForColumn:@"fileID"];
        NSString *url = [result stringForColumn:@"url"];
        NSString *size = [result stringForColumn:@"size"];
        NSString *type = [result stringForColumn:@"type"];
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:idStr,@"id",fileName,@"fileName",fileID,@"fileID",url,@"url",size,@"size",type,@"type", nil];
        [data addObject:dic];
    }
    
    if (![_db close]) {
        NSLog(@"数据库没有关闭!");
        return nil;
    }
    
    return data;
}

//自定义文件-插入文件数据
- (BOOL)insertTableWithId:(NSString *)idStr withFileName:(NSString *)fileName withFileID:(NSString *)fileID withUrl:(NSString *)url withSize:(NSString *)size withType:(NSString *)type
{
    NSString *sql = [NSString stringWithFormat:@"insert into Main_table(id,fileName,fileID,url,size,type) values('%@','%@','%@','%@','%@','%@')",idStr,fileName,fileID,url,size,type];

    if (![_db open]) {
        NSLog(@"数据库没有打开!");
        return NO;
    }
    
    [_db setShouldCacheStatements:YES];
    
    if (![_db tableExists:@"Main_table"]) {
        if (![self createMainTable:@"Main_table" byDB:_db]) {
            NSLog(@"表单不存在!");
            return NO;
        }
    }
    
    BOOL isUpdate = [_db executeUpdate:sql];
    
    if (![_db close]) {
        NSLog(@"数据库没有关闭!");
        return NO;
    }
    
    return isUpdate;
}

//自定义文件-删除文件数据
- (BOOL)deleteCunstomFileID:(NSString *)CateID
{
    NSString *sql = [NSString stringWithFormat:@"delete from Main_table where fileID='%@'",CateID];
    if (![_db open]) {
        NSLog(@"数据库没有打开!");
        return NO;
    }
    
    [_db setShouldCacheStatements:YES];
    
    if (![_db tableExists:@"Main_table"]) {
        if (![self createMainTable:@"Main_table" byDB:_db]) {
            NSLog(@"表单不存在!");
            return NO;
        }
    }
    
    BOOL isUpdate = [_db executeUpdate:sql];
    
    if (![_db close]) {
        NSLog(@"数据库没有关闭!");
        return NO;
    }
    
    return isUpdate;
}

/*
 create table xx(id integer primary key,phone text)
 insert into xx(id,phone) values(?,?)
 select * from xx where phone = ?
 delete from xx where phone = ?
 update xx set nick = ? where phone=?
 */
@end
